﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Indigo.Quibbler.Properties;

namespace Indigo.Quibbler.Controls
{
    /// <summary>
    /// Interaction logic for AdsControl.xaml
    /// </summary>
    public partial class ArticleControl : UserControl
    {
        #region Properties
        public string Headline
        {
            get { return (string)GetValue(HeadlineProperty); }
            set { SetValue(HeadlineProperty, value); }
        }
        public string SubHeadline
        {
            get { return (string)GetValue(SubHeadlineProperty); }
            set { SetValue(SubHeadlineProperty, value); }
        }

        public string PubDate
        {
            get { return (string)GetValue(PubDateProperty); }
            set { SetValue(PubDateProperty, value); }
        }

        public string SyncTime
        {
            get { return (string)GetValue(SyncTimeProperty); }
            set { SetValue(SyncTimeProperty, value); }
        }

        #endregion

        #region Dependency Properties
        public static readonly DependencyProperty HeadlineProperty = DependencyProperty.Register("Headline", typeof(string), typeof(ArticleControl));
        public static readonly DependencyProperty SubHeadlineProperty = DependencyProperty.Register("SubHeadline", typeof(string), typeof(ArticleControl));
        public static readonly DependencyProperty PubDateProperty = DependencyProperty.Register("PubDate", typeof(string), typeof(ArticleControl));
        public static readonly DependencyProperty SyncTimeProperty = DependencyProperty.Register("SyncTime", typeof(string), typeof(ArticleControl),new PropertyMetadata(OnSyncTimeChanged));
        #endregion



        public string ImageUrl
        {
            get { return (string)GetValue(ImageUrlProperty); }
            set { SetValue(ImageUrlProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImageUrl.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageUrlProperty =
            DependencyProperty.Register("ImageUrl", typeof(string), typeof(ArticleControl), new PropertyMetadata(OnImageUrlChanged));


        #region Static Handlers
        private static void OnImageUrlChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            (obj as ArticleControl).OnImageUrlChanged(e);
        }
        private static void OnSyncTimeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            (obj as ArticleControl).OnSyncTimeChanged(e);
        }
        #endregion

        private void OnImageUrlChanged(DependencyPropertyChangedEventArgs e)
        {
            if (string.IsNullOrEmpty(e.NewValue.ToString()))
            {
                txtSubHeadline.SetValue(Grid.ColumnSpanProperty, 2);
                imgArticle.Visibility = Visibility.Hidden;
            }
            else
            {
                txtSubHeadline.SetValue(Grid.ColumnSpanProperty, 1);
                imgArticle.Visibility = Visibility.Visible;
            }
        }

        private void OnSyncTimeChanged(DependencyPropertyChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.NewValue.ToString()))
            {
                if ((DateTime.Now - (new DateTime(long.Parse(e.NewValue.ToString())))).TotalMinutes <= Settings.Default.ContentUpdateFrequency)
                {
                    //Highlight the content. Its new. Oringinal color: #BF3B3B3B
                    borderArticle.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#BF81A2FC"));
                    txtHeadline.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFEF832C"));
                    //txtSubHeadline.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC8C8C8"));
                }
            }
        }

        #region Constructor
        public ArticleControl()
        {
            InitializeComponent();
        }
        #endregion
    }
}

